/**
 * 时时彩的共用逻辑
 */
(function() {
    var currTabId = 1;
    var tabDraws = [
        // 1：两面
        fillLM,
        
        // 1-5球
        fill_table,

        // 前中后
        fill_qzh,
    ];

    function fillLM() {
        var types = ["大", "小", "单", "双"];
        for (var i = 0; i < types.length; i++) {
            var t = types[i];
            add_bet_item('和' + t, '总和' + t, $(".zonghe"));
        }
        var types = ["龙", "虎", "和"];
        for (var i = 0; i < types.length; i++) {
            var t = types[i];
            add_bet_item(t, t, $(".longhu"));
        }
    
        // 5球的大小单双
        var ths = ["一", "二", "三", "四", "五"];
        var types = ["大", "小", "单", "双"];
        var div = $(".page1 .five");
        div.empty();
        for (var j = 0; j < ths.length; j++) {
            add_bet_th("第" + ths[j] + "球", div);
    
            var ul = $('<ul class="am-avg-sm-2 bet"></ul>');
            for (var i = 0; i < types.length; i++) {
                var t = types[i];
                add_bet_item(ths[j] + t, t, ul);
            }
            div.append(ul);
        }
    }

    function fill_qzh() {
        // 前中后
        var ths = ["前", "中", "后"];
        var types = ['豹子', '顺子', '对子', '半顺'];
        var div = $(".qzh");
        div.empty();
        for (var j in ths) {
            add_bet_th(ths[j] + "三", div);

            var ul = $('<ul class="am-avg-sm-2 bet"></ul>');
            for (var i in types) {
                add_bet_item(ths[j] + types[i][0], types[i], ul);
            }
            div.append(ul);

            var ul = $('<ul class="am-avg-sm-1 bet"></ul>');
            add_bet_item(ths[j] + "杂", "杂六", ul);
            div.append(ul);
        }
    }

    function fill_table() {
        // 5球的数字
        var ths = ["一", "二", "三", "四", "五"];
        var div = $(".page2 .five");
        div.empty();
        for (var j = 0; j < ths.length; j++) {
            add_bet_th("第" + ths[j] + "球", div);

            var ul = $('<ul class="am-avg-sm-2 bet"></ul>');
            for (var i = 0; i < 10; i++) {
                var t = '<span class="bet-content round-1">' + i + '</span>'
                add_bet_item(ths[j] + i, t, ul);
            }
            div.append(ul);
        }
    }

    var drawF2 = function() {
        var c = Result.last();
        if (!c || !c.red) return [];
        var red = c.red;
        var r = [];

        var he = gameRule.he(red);
        r.push(he);

        if (gameRule.isHeBig(red)) r.push('大');
        else r.push('小');
        if (gameRule.isHeOdd(red)) r.push('单');
        else r.push('双');

        if (gameRule.isLong(red)) 
            r.push('龙');
        else if (gameRule.isHu(red))
            r.push('虎');
        else if (gameRule.isHe(red))
            r.push('和');

        return r;
    }

    var drawF  = function(i) {
        i = parseInt(i);
        return '<span class="round">' + i + '</span>';
    }

    // 检测能否下注，看下单号1-10是否满足要求
    var checkF = function(type, issue, data) {
        var limit = Util.getNumberLimit(type);
        if (limit <= 0 || limit >= 10) return true;

        var arr1 = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十'];
        var arr2 = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
        var m = {};
        for (var k in data) {
            if (k.length != 2) continue;
            if (arr1.indexOf(k[0]) != -1 && arr2.indexOf(k[1]) != -1) {
                if (!m[k[0]]) m[k[0]] = {};
                m[k[0]][k[1]] = true;
            }
        }
        if (Util.isEmpty(m)) return true;

        // 获取下已经下注的单据
        var bets = context.bets[type];
        if (bets) bets = bets[issue] || [];
        else bets = [];
        for (var i in bets) {
            var k = bets[i].target;
            if (k.length != 2) continue;
            if (arr1.indexOf(k[0]) != -1 && arr2.indexOf(k[1]) != -1) {
                if (!m[k[0]]) m[k[0]] = {};
                m[k[0]][k[1]] = true;
            }
        }

        var total = 0;

        for (var k in m) {
            var m2 = m[k];
            if (Object.keys(m2).length > limit) {
                var l = "下注失败，__g__ 定位玩法，单个名次不允许下注__v__码或者__v__以上！如有疑问，请咨询客服！";
                l = l.replace(/__g__/, get_game_display_name(type));
                l = l.replace(/__v__/g, limit);
                alert2(l, true);
                return false;
            }
        }

        return true;
    }

    var initF = function(game, first) {
        window.game = game;
        initGame(game, function() {
            if (first !== false) fill_tabs(['两面', '1-5球', '前中后']);
            currTabId = parseInt($(".menu-left a.active").attr("value"));
            $(".page" + currTabId).attr("flag", 1);
            var f = tabDraws[currTabId - 1];
            f();
        }, first);
    }

    var data = {
        drawResult: drawF,
        drawResult2: drawF2,
        init: initF,
        checkBetList: checkF,
    }
    context.global.game["cqssc"] = data;
    context.global.game["xjssc"] = data;
    context.global.game["tjssc"] = data;
    context.global.game["zyssc"] = data;
    context.global.game["ffcqq"] = data;
})();